﻿using DemoQLKhoSX.BusinessLogicLayer;
using DemoQLKhoSX.EntityClasses;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DemoQLKhoSX.PresentationLayer
{
    public partial class FrmXuat : Form
    {
        private XuatBLO CTXuat = new XuatBLO();
        private List<XuatEntity> dsXuat = new List<XuatEntity>();
        private DataTable tblXuat = new DataTable();
        private int _State;
        public int State
        {
            set
            {
                _State = value;
                if (_State == 0)//Xem
                {
                    btnHuy.Visible = false;
                    btnLuu.Visible = false;
                    btnSua.Visible = true;
                    btnThem.Visible = true;
                    btnTim.Visible = true;
                    btnXoa.Visible = true;
                    dgvChiTiet.ReadOnly = true;
                    groupBox1.Enabled = false;
                }
                else//Có thể lưu
                {
                    btnHuy.Visible = true;
                    btnLuu.Visible = true;
                    btnSua.Visible = false;
                    btnThem.Visible = false;
                    btnTim.Visible = false;
                    btnXoa.Visible = false;
                    dgvChiTiet.ReadOnly = false;
                    groupBox1.Enabled = true;
                }
            }
            get
            {
                return _State;
            }

        }

        public FrmXuat()
        {
            InitializeComponent();
            State = 0;
            LoadComboDefault();
            if (tblXuat == null)
                tblXuat = new DataTable();
            tblXuat.Columns.Add("IDXuat", Type.GetType("System.String"));
            tblXuat.Columns.Add("NgayXuat", Type.GetType("System.DateTime"));
            tblXuat.Columns.Add("IDKhachHang", Type.GetType("System.String"));
            tblXuat.Columns.Add("SoLuongXuat", Type.GetType("System.Int32"));
            tblXuat.Columns.Add("IDThuKho", Type.GetType("System.String"));
            tblXuat.Columns.Add("IDHang", Type.GetType("System.String"));
            tblXuat.Columns.Add("TenHang", Type.GetType("System.String"));

            DataColumn[] khoa = new DataColumn[4];
            khoa[0] = tblXuat.Columns["IDXuat"];
            khoa[1] = tblXuat.Columns["IDKhachHang"];
            khoa[2] = tblXuat.Columns["IDThuKho"];
            khoa[3] = tblXuat.Columns["IDHang"];
            tblXuat.PrimaryKey = khoa;
            dgvChiTiet.DataSource = tblXuat;
        }

        KhachHangBLO khachs = new KhachHangBLO();
        ThuKhoBLO thukhos = new ThuKhoBLO();
        HangBLO hangs = new HangBLO();

        List<ThuKhoEntity> dsthukho;
        List<KhachHangEntity> dskhachhang;
        List<HangEntity> dsHang;
        void LoadComboDefault()
        {
            if (dsthukho == null)
                dsthukho = thukhos.getThuKho();
            if (dskhachhang == null)
                dskhachhang = khachs.getKhachHang();
            if (dsHang == null)
                dsHang = hangs.getHang();

            cboKhachHang.DataSource = dskhachhang;
            cboKhachHang.DisplayMember = "TenKH";
            cboKhachHang.ValueMember = "IDKhachHang";

            cboThuKho.DataSource = dsthukho;
            cboThuKho.DisplayMember = "TenThuKho";
            cboThuKho.ValueMember = "IDThuKHo";

            cboChonHang.DataSource = dsHang;
            cboChonHang.DisplayMember = "TenHang";
            cboChonHang.ValueMember = "IDHang";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            State = 1;
            dsXuat = new List<XuatEntity>();
            tblXuat.Rows.Clear();
            txtSoPhieu.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            State = 1;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            State = 0;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            State = 0;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            foreach (XuatEntity x in dsXuat)
            {
                CTXuat.InsertXuat(x.IDXuat, x.IDKhachHang.IDKhachHang, x.IDThuKho.IDThuKHo, x.IDHang.IDHang, x.NgayXuat, x.SoLuongXuat);
            }
            State = 0;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            State = 0;
            txtSoPhieu.Text = "";
            tblXuat.Rows.Clear();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (State == 0)
                return;
            if (txtSoPhieu.Text.Trim() == "")
            {
                MessageBox.Show("Phải nhập số phiếu");
                return;
            }
            XuatEntity newXuat = new XuatEntity();
            //Thieu kiem tra ton tai + them so luong xuat
            newXuat.IDXuat = txtSoPhieu.Text;
            newXuat.NgayXuat = dateTimePicker1.Value;
            newXuat.IDKhachHang = khachs.getKhachHangByID(cboKhachHang.SelectedValue.ToString());
            newXuat.IDThuKho = thukhos.getThuKhoByID(cboThuKho.SelectedValue.ToString());
            newXuat.IDHang = hangs.getHangByID(cboChonHang.SelectedValue.ToString());
            newXuat.SoLuongXuat = Convert.ToInt32(numericUpDown1.Value);
            dsXuat.Add(newXuat);

            DataRow newr;
            newr = tblXuat.Rows.Find(new object[] { newXuat.IDXuat, newXuat.IDKhachHang.IDKhachHang, newXuat.IDThuKho.IDThuKHo, newXuat.IDHang.IDHang });
            if (newr == null)
            {
                newr = tblXuat.NewRow();
                newr["IDXuat"] = newXuat.IDXuat;
                newr["NgayXuat"] = newXuat.NgayXuat;
                newr["IDKhachHang"] = newXuat.IDKhachHang.IDKhachHang;
                newr["IDThuKho"] = newXuat.IDThuKho.IDThuKHo;
                newr["IDHang"] = newXuat.IDHang.IDHang;
                newr["SoLuongXuat"] = newXuat.SoLuongXuat;
                newr["TenHang"] = newXuat.IDHang.TenHang;
                tblXuat.Rows.Add(newr);
            }
            else
            {
                newr["SoLuongXuat"] = Convert.ToInt32(newr["SoLuongXuat"]) + newXuat.SoLuongXuat;
            }
            dgvChiTiet.DataSource = tblXuat;
        }
    }
}
